<template>
  <div class="app-container">
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单类型" prop="serialNo">
        <el-select @change="change" v-model="queryParams.type" placeholder="请选择订单类型" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="item in orderType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="orderList">
      <el-table-column width="55" align="center" />
      <!-- <el-table-column label="用户ID" align="center" prop="id" /> -->
       <!-- 酒店订单列表 -->
      <el-table-column key="1" v-if="type===1" label="酒店名称" align="center" prop="hotelName" />
      <el-table-column key="2" v-if="type===1" label="房型名称" align="center" prop="detailName" />
      <el-table-column key="3" v-if="type===1" label="下单时房间密码" align="center" prop="autoPwd" />
      <el-table-column key="4" v-if="type===1" label="修改后新密码" align="center" prop="adminPwd" />
      <el-table-column key="5" v-if="type===1" label="房间号" align="center" prop="roomNo" />
      <el-table-column key="6" v-if="type===1" label="开始时间" align="center" prop="startTime" />
      <el-table-column key="7" v-if="type===1" label="结束时间" align="center" prop="endTime" />
      
      
      <!-- 钻石套餐订单列表 -->
      <el-table-column key="8" v-if="type===2" label="套餐名称" align="center" prop="name" />
      <el-table-column key="9" v-if="type===2" label="套餐价格" align="center" prop="price" />
      <el-table-column key="10" v-if="type===2" label="下单时间" align="center" prop="createTime" />

      <!-- 会员套餐订单列表 -->
      <el-table-column key="11" v-if="type===3" label="套餐名称" align="center" prop="name" />
      <el-table-column key="12" v-if="type===3" label="套餐价格" align="center" prop="price" />
      <el-table-column key="13" v-if="type===3" label="下单时间" align="center" prop="createTime" />
      <!-- <el-table-column label="酒店剩余空房总数" align="center" prop="enabled" /> -->
      <el-table-column v-if="type===1" prop="status" label="状态" width="80">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 0">未支付</el-tag>
          <el-tag type="info" v-if="scope.row.status == 1">已支付</el-tag>
          <el-tag v-if="scope.row.status == 2">已兑换</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 3">已退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="type===1">
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleUpdate(scope.row)">编辑</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-goods" @click="handleResetPwd(scope.row)">重置密码</el-button>
          <!-- v-if="scope.row.enableReset" -->
          <el-button size="mini" type="text" icon="el-icon-goods" @click="handleSetRoom(scope.row)">调整房间</el-button>
          <el-button size="mini" type="text" icon="el-icon-goods" @click="handleRefund(scope.row)">退款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="房间" prop="room">
              <el-select v-model="selectedObj" value-key="id" data="citationList" @change="roomChange">
                <el-option v-for="item in rooms" :key="item.value" :label="item.label" :value="item">
                  <!-- 绑定整个对象 :ml-citation{ref="1,4" data="citationList"} -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { getOrderList, GetDetail, ResetPwd, SetRoom, Refund } from "@/api/order";
import { getRoomList } from '@/api/common'
export default {
  name: "order",
  data () {
    return {
      title: '',
      selectedObj: null,
      // 遮罩层
      loading: true,
      open: false,
      // 选中数组
      ids: [],
      form: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户数据
      orderList: [],
      rooms: [],
      orderId: "",
      type:1,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 1
      },
      orderType: [{
        value: 1,
        label: '兑换酒店'
      },
      {
        value: 2,
        label: '钻石套餐'
      },
      {
        value: 3,
        label: '会员套餐'
      }]
    }
  },
  created () {
    this.getList();
  },
  computed: {},
  methods: {
    roomChange (e) {
      this.form = e
    },
    change(e){
      this.type=e
      this.queryParams.type=e
      this.getList()
    },
    getRoomList () {
      getRoomList().then((response) => {
        this.rooms = response.data
      });
    },
    handleSetRoom (row) {
      this.open = true
      this.orderId = row.id
      this.getRoomList()
    },
    handleResetPwd (row) {
      ResetPwd({ orderId: row.id }).then((response) => {
        this.$modal.msgSuccess("重置密码成功");
        this.getList();
      });
    },

    /** 查询酒店列表 */
    getList () {
      this.loading = true;
      getOrderList(this.queryParams).then((response) => {
        this.orderList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => {
        return item.id;
      });
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleRefund (row) {

      this.$modal
        .confirm("是否确认退款？")
        .then(function () {
          return Refund({ orderId: row.id });
        })
        .then(() => {
          this.$modal.msgSuccess("退款成功");
          this.getList();
        })
        .catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      GetDetail(id).then((response) => {
        this.form = response.data
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const params = {
            orderId: this.orderId,
            roomNo: this.form.label,
            deviceId: this.form.deviceId,
            roomTypeId: this.form.roomTypeId,
            roomTypeName: this.form.roomTypeName
          }
          SetRoom(params).then(res => {
            this.$modal.msgSuccess("修改成功");
            this.cancel()
          })
        }
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.selectedObj = null
      this.resetForm("form");
    },
  },
};
</script>
<style scoped lang="scss">
.modal-wrap {
  ::v-deep .el-dialog__body {
    height: 600px;
    overflow-y: scroll;
  }
}

.avatar-uploader {
  ::v-deep .el-upload {
    width: 100px;
    height: 100px;
    border: 1px dotted #eee;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .avatar-uploader-icon {
    font-size: 24px;
  }
}

.banner-img {
  width: 100px;
  height: 100px;
  // margin-left: 20px;
}

.editBox {
  display: flex;
  justify-content: space-between;
}
</style>
